/**
 * SpringMVC restful 风格
 * 发票控制器，控制关于发票管理的一些业务逻辑操作
 * @author zhangq
 * @date 2015-03-18
 */
package com.chnbst.bstyun.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chnbst.bstyun.core.common.Pager;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.model.pojo.Invoice;
import com.chnbst.bstyun.model.vo.InvoiceModelVo;
import com.chnbst.bstyun.model.vo.InvoiceVo;
import com.chnbst.bstyun.service.InvoiceModelService;
import com.chnbst.bstyun.service.InvoiceService;


/**
 * 控制器注解 请求映射注解
 * controller 代表类是控制器
 * restcontroller 代表类是控制器， 所有的方法 都是ajax方法
 * @author zhangq
 *
 */

@Controller
@RequestMapping("/sysAdmin")
public class InvoiceAdminController {
	//每页显示的用户数
	private int pageSize = 10;
	//用户列表页码
	private int currPage = 1;
	
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private InvoiceService invoiceService;
	@Autowired
	private InvoiceModelService invoiceModelService;
	
	/**
	 * @return 后台发票管理页面
	 * @throws Exception
	 */
	@RequestMapping(value="/invoice/invoiceList",method=RequestMethod.GET)
	public ModelAndView invoiceList()throws Exception{
		return new ModelAndView("admin/financeManager/invoiceManager");
	}
	
	/**
	 * Ajax 分页查询发票列表list
	 * @return 发票列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/invoice/invoiceList/search",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> invoiceList(String page,String last,HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(SignUtil.isNotEmpty(page)) {
			currPage = Integer.valueOf(page);
		}
		
		if(SignUtil.isNotEmpty(last)) {
			pageSize = Integer.valueOf(last);
		}
		
		int totalRows = invoiceService.selectInvoiceCount(map);
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < currPage) {
			currPage = pageCount;
		}
		int start = Pager.getStart(currPage, pageSize);
		
		map.put("start", start);
		map.put("pageSize", pageSize);
		map.put("sortField", "add_time");
		map.put("sortOrder", "desc");
		
		List<InvoiceVo> invoiceList = invoiceService.selectInvoiceVos(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("page", currPage);
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("invoiceList", invoiceList);
		
		return resultMap;
	}
	  
	/**
	 * 根据ID查询发票的详细信息
	 * @return 发票详情页面
	 * @throws Exception
	 */
	@RequestMapping(value="/invoice/invoiceDetail/{invoiceId}",method=RequestMethod.GET)
	public ModelAndView supplierDetail(@PathVariable Integer invoiceId)throws Exception{
		
		ModelAndView mav = new ModelAndView();
		
		InvoiceVo invoiceInfo = invoiceService.selectInvoiceVo(invoiceId);
		
		mav.addObject("invoiceInfo", invoiceInfo);
		mav.setViewName("admin/financeManager/invoiceDetail");
		
		return mav;
	}

	/**
	 * 根据ID修改发票信息
	 * @return true 或者false
	 * @throws Exception
	 */
	@RequestMapping(value="/operate/invoice/updateInvoice",method=RequestMethod.POST)
	@ResponseBody
	public boolean updateInvoice(HttpServletRequest request,Integer invoiceId,
			String deliveryNum,String deliveryType) throws Exception{
		boolean isSuccess;
		Invoice invoice = invoiceService.selectInvoice(invoiceId);
		
		invoice.setDeliveryNum(deliveryNum);
		invoice.setDeliveryType(deliveryType);
		invoice.setInvoiceState("2");
		
		isSuccess = invoiceService.updateInvoice(invoice);
		
		return isSuccess;
	}
	
	/**
	 * @return 后台发票抬头管理页面
	 * @throws Exception
	 */
	@RequestMapping(value="/invoice/invoiceModelList",method=RequestMethod.GET)
	public ModelAndView invoiceModelList()throws Exception{
		return new ModelAndView("admin/financeManager/invoiceModelManager");
	}
	
	/**
	 * Ajax 分页查询发票抬头列表list
	 * @return 发票抬头列表页面
	 * @throws Exception
	 */
	@RequestMapping(value="/invoice/invoiceModelList/search",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> invoiceModelList(String page,String last,HttpServletRequest request)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		
		if(SignUtil.isNotEmpty(page)) {
			currPage = Integer.valueOf(page);
		}
		
		if(SignUtil.isNotEmpty(last)) {
			pageSize = Integer.valueOf(last);
		}
		
		int totalRows = invoiceModelService.selectInvoiceModelCount(map);
		int pageCount = Pager.getPageCount(totalRows, pageSize);
		if(pageCount < currPage) {
			currPage = pageCount;
		}
		int start = Pager.getStart(currPage, pageSize);
		
		map.put("start", start);
		map.put("pageSize", pageSize);
		map.put("sortField", "add_time");
		map.put("sortOrder", "desc");
		
		List<InvoiceModelVo> invoiceModelList = invoiceModelService.selectInvoiceModelVos(map);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("page", currPage);
		resultMap.put("pageCount", pageCount);
		resultMap.put("totalRows", totalRows);
		resultMap.put("invoiceModelList", invoiceModelList);
		
		return resultMap;
	}
	  
	/**
	 * 根据ID查询发票的详细信息
	 * @return 发票详情页面
	 * @throws Exception
	 */
	@RequestMapping(value="/invoice/invoiceModelDetail/{invoiceModelId}",method=RequestMethod.GET)
	public ModelAndView invoiceModelDetail(@PathVariable Integer invoiceModelId)throws Exception{
		
		ModelAndView mav = new ModelAndView();
		
		InvoiceModelVo invoiceModelInfo = invoiceModelService.selectInvoiceModelVo(invoiceModelId);
		
		mav.addObject("invoiceModelInfo", invoiceModelInfo);
		mav.setViewName("admin/financeManager/invoiceModelDetail");
		
		return mav;
	}
}
